package com.ruicar.afs.cloud.common.aspect;

import com.ruicar.afs.cloud.common.data.mybatis.plugin.datascope.aop.AopDataScopeHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 合同系统全局数据权限切面处理
 * 如果是当前登陆人是渠道角色，则只允许查看自己报过的案件，如果是业务人员登录则允许查看全部案件
 * 根据渠道id进行区分判断
 */
@Slf4j
@Aspect
@Component
public class DataAccessAspect {

    @Around("@annotation(dataAccess)")
    public Object around(ProceedingJoinPoint point, DataAccess dataAccess) throws Throwable {
        String strClassName = point.getTarget().getClass().getName();
        String strMethodName = point.getSignature().getName();
        log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName);

        try{
            String[] tables = dataAccess.tables();
            List<String> tableName = new ArrayList<>();
            Arrays.asList(tables);
            String columnName = "channel_id";
            String scopeValue = dataAccess.key();
            AopDataScopeHolder.pushAopScope(tableName,columnName,scopeValue);
            Object obj = point.proceed();
            return obj;
        }finally {
            AopDataScopeHolder.popAopScope();
        }

    }

}
